/*
 * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
 */
package ocr.ocr.ui.service;

import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_ACCURATE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_ACCURATE_BASIC;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_BUSINESSCARD;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_BUSINESS_LICENSE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_CAR_SELL_INVOICE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_CUSTOM;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_DRIVING_LICENSE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_EXAMPLE_DOC_REG;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_GENERAL;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_GENERAL_BASIC;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_GENERAL_ENHANCED;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_GENERAL_WEBIMAGE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_HANDWRITING;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_HUKOU_PAGE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_LICENSE_PLATE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_LOTTERY;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_MEDICAL_DETAIL;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_NORMAL_MACHINE_INVOICE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_NUMBERS;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_ONLINE_TAXI_ITINERARY;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_PASSPORT;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_QRCODE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_RECEIPT;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_TAXIRECEIPT;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_TRAINTICKET;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_TRIP_TICKET;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_VATINVOICE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_VEHICLE_LICENSE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_VIHICLE_SERTIFICATION;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_VINCODE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_WEIGHT_NOTE;
import static com.zjw.zy.coreui.fragment.ocr.OcrFragment.REQUEST_CODE_WRITTEN_TEXT;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Pair;

import androidx.annotation.Nullable;

import com.baidu.ocr.sdk.OCR;
import com.baidu.ocr.sdk.OnResultListener;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.model.BankCardParams;
import com.baidu.ocr.sdk.model.BankCardResult;
import com.baidu.ocr.sdk.model.GeneralBasicParams;
import com.baidu.ocr.sdk.model.GeneralParams;
import com.baidu.ocr.sdk.model.GeneralResult;
import com.baidu.ocr.sdk.model.OcrRequestParams;
import com.baidu.ocr.sdk.model.OcrResponseResult;
import com.baidu.ocr.sdk.model.Word;
import com.baidu.ocr.sdk.model.WordSimple;
import com.zjw.zy.R;
import com.zjw.zy.coreui.dialog.LoadingDialog;
import com.zjw.zy.manager.PageManager;
import com.zjw.zy.utils.AppUtils;
import com.zjw.zy.utils.PathUtils;
import com.zjw.zy.utils.ResourcesUtils;
import com.zjw.zy.utils.ToastUtils;

import java.io.File;

import ocr.ocr.ui.util.OcrTextUtils;

public final class OrcRecognizeService {

    private static final String TAG = "OrcRecognizeService";

    private static LoadingDialog loadingDialog;

    private interface ServiceListener {
        void onResult(String result);
    }

    private static void onParseResult(String result, int requestCode) {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
        switch (requestCode) {
            case REQUEST_CODE_ACCURATE:
                //结果
                Pair<String, Float> pair = OcrTextUtils.parseHighAndLocation(result);
                if (pair == null) {
                    ToastUtils.showLong("识别失败");
                    return;
                }
                PageManager.goText(pair.first, pair.second);
                break;
            case REQUEST_CODE_GENERAL_BASIC:
                String r = OcrTextUtils.parseBasicText(result);
                PageManager.goText(r, null);
                break;
        }
    }

    private static void showLoading() {
        loadingDialog = new LoadingDialog(AppUtils.getMainActivity()).
                title(ResourcesUtils.getString(R.string.orc_loading_text));

        loadingDialog.show();
    }

    public static boolean processActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        // 识别成功回调，通用文字识别（含位置信息）
        if (requestCode == REQUEST_CODE_GENERAL && resultCode == Activity.RESULT_OK) {
            showLoading();
            recGeneral(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });

            return true;
        }

        // 识别成功回调，通用文字识别（含位置信息高精度版）
        if (requestCode == REQUEST_CODE_ACCURATE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recAccurate(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，通用文字识别
        if (requestCode == REQUEST_CODE_GENERAL_BASIC && resultCode == Activity.RESULT_OK) {
            showLoading();
            recGeneralBasic(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，通用文字识别（高精度版）
        if (requestCode == REQUEST_CODE_ACCURATE_BASIC && resultCode == Activity.RESULT_OK) {
            showLoading();
            recAccurateBasic(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，通用文字识别（含生僻字版）
        if (requestCode == REQUEST_CODE_GENERAL_ENHANCED && resultCode == Activity.RESULT_OK) {
            showLoading();
            recGeneralEnhanced(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，网络图片文字识别
        if (requestCode == REQUEST_CODE_GENERAL_WEBIMAGE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recWebimage(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，行驶证识别
        if (requestCode == REQUEST_CODE_VEHICLE_LICENSE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recVehicleLicense(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，驾驶证识别
        if (requestCode == REQUEST_CODE_DRIVING_LICENSE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recDrivingLicense(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，车牌识别
        if (requestCode == REQUEST_CODE_LICENSE_PLATE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recLicensePlate(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，营业执照识别
        if (requestCode == REQUEST_CODE_BUSINESS_LICENSE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recBusinessLicense(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，通用票据识别
        if (requestCode == REQUEST_CODE_RECEIPT && resultCode == Activity.RESULT_OK) {
            showLoading();
            recReceipt(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，出租车票
        if (requestCode == REQUEST_CODE_TAXIRECEIPT && resultCode == Activity.RESULT_OK) {
            showLoading();
            recTaxireceipt(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，VIN码
        if (requestCode == REQUEST_CODE_VINCODE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recVincode(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，火车票
        if (requestCode == REQUEST_CODE_TRAINTICKET && resultCode == Activity.RESULT_OK) {
            showLoading();
            recTrainticket(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，护照
        if (requestCode == REQUEST_CODE_PASSPORT && resultCode == Activity.RESULT_OK) {
            showLoading();
            recPassport(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，二维码
        if (requestCode == REQUEST_CODE_QRCODE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recQrcode(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，彩票
        if (requestCode == REQUEST_CODE_LOTTERY && resultCode == Activity.RESULT_OK) {
            showLoading();
            recLottery(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，增值税发票
        if (requestCode == REQUEST_CODE_VATINVOICE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recVatInvoice(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，数字
        if (requestCode == REQUEST_CODE_NUMBERS && resultCode == Activity.RESULT_OK) {
            showLoading();
            recNumbers(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，手写
        if (requestCode == REQUEST_CODE_HANDWRITING && resultCode == Activity.RESULT_OK) {
            showLoading();
            recHandwriting(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，名片
        if (requestCode == REQUEST_CODE_BUSINESSCARD && resultCode == Activity.RESULT_OK) {
            showLoading();
            recBusinessCard(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，自定义模板
        if (requestCode == REQUEST_CODE_CUSTOM && resultCode == Activity.RESULT_OK) {
            showLoading();
            recCustom(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，行程单识别
        if (requestCode == REQUEST_CODE_TRIP_TICKET && resultCode == Activity.RESULT_OK) {
            showLoading();
            recTripTicket(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，机动车销售发票
        if (requestCode == REQUEST_CODE_CAR_SELL_INVOICE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recCarSellInvoice(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，车辆合格证
        if (requestCode == REQUEST_CODE_VIHICLE_SERTIFICATION && resultCode == Activity.RESULT_OK) {
            showLoading();
            recVihicleCertification(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，试卷分析和识别
        if (requestCode == REQUEST_CODE_EXAMPLE_DOC_REG && resultCode == Activity.RESULT_OK) {
            showLoading();
            recExampleDoc(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，手写文字识别
        if (requestCode == REQUEST_CODE_WRITTEN_TEXT && resultCode == Activity.RESULT_OK) {
            showLoading();
            recWrittenText(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，户口本识别
        if (requestCode == REQUEST_CODE_HUKOU_PAGE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recHuKouPage(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，通用机打发票识别
        if (requestCode == REQUEST_CODE_NORMAL_MACHINE_INVOICE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recNormalMachineInvoice(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，磅单识别
        if (requestCode == REQUEST_CODE_WEIGHT_NOTE && resultCode == Activity.RESULT_OK) {
            showLoading();
            recweightnote(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，医疗费用明细识别
        if (requestCode == REQUEST_CODE_MEDICAL_DETAIL && resultCode == Activity.RESULT_OK) {
            showLoading();
            recmedicaldetail(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }

        // 识别成功回调，网约车行程单识别
        if (requestCode == REQUEST_CODE_ONLINE_TAXI_ITINERARY && resultCode == Activity.RESULT_OK) {
            showLoading();
            reconlinetaxiitinerary(AppUtils.getApp(), PathUtils.genericOrcImageFilePath(),
                    new ServiceListener() {
                        @Override
                        public void onResult(String result) {
                            onParseResult(result, requestCode);
                        }
                    });
            return true;
        }
        return false;
    }

    private static void recGeneral(Context ctx, String filePath, final ServiceListener listener) {
        GeneralParams param = new GeneralParams();
        param.setDetectDirection(true);
        param.setVertexesLocation(true);
        param.setRecognizeGranularity(GeneralParams.GRANULARITY_SMALL);
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeGeneral(param, new OnResultListener<GeneralResult>() {
            @Override
            public void onResult(GeneralResult result) {
                StringBuilder sb = new StringBuilder();
                for (WordSimple wordSimple : result.getWordList()) {
                    Word word = (Word) wordSimple;
                    sb.append(word.getWords());
                    sb.append("\n");
                }
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recAccurate(Context ctx, String filePath, final ServiceListener listener) {
        GeneralParams param = new GeneralParams();
        param.setDetectDirection(true);
        param.setVertexesLocation(true);
        param.setRecognizeGranularity(GeneralParams.GRANULARITY_SMALL);
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeAccurate(param, new OnResultListener<GeneralResult>() {
            @Override
            public void onResult(GeneralResult result) {
                StringBuilder sb = new StringBuilder();
                for (WordSimple wordSimple : result.getWordList()) {
                    Word word = (Word) wordSimple;
                    sb.append(word.getWords());
                    sb.append("\n");
                }
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recAccurateBasic(Context ctx, String filePath, final ServiceListener listener) {
        GeneralParams param = new GeneralParams();
        param.setDetectDirection(true);
        param.setVertexesLocation(true);
        param.setRecognizeGranularity(GeneralParams.GRANULARITY_SMALL);
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeAccurateBasic(param, new OnResultListener<GeneralResult>() {
            @Override
            public void onResult(GeneralResult result) {
                StringBuilder sb = new StringBuilder();
                for (WordSimple wordSimple : result.getWordList()) {
                    WordSimple word = wordSimple;
                    sb.append(word.getWords());
                    sb.append("\n");
                }
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }


    private static void recGeneralBasic(Context ctx, String filePath, final ServiceListener listener) {
        GeneralBasicParams param = new GeneralBasicParams();
        param.setDetectDirection(true);
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeGeneralBasic(param, new OnResultListener<GeneralResult>() {
            @Override
            public void onResult(GeneralResult result) {
                StringBuilder sb = new StringBuilder();
                for (WordSimple wordSimple : result.getWordList()) {
                    WordSimple word = wordSimple;
                    sb.append(word.getWords());
                    sb.append("\n");
                }
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recGeneralEnhanced(Context ctx, String filePath, final ServiceListener listener) {
        GeneralBasicParams param = new GeneralBasicParams();
        param.setDetectDirection(true);
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeGeneralEnhanced(param, new OnResultListener<GeneralResult>() {
            @Override
            public void onResult(GeneralResult result) {
                StringBuilder sb = new StringBuilder();
                for (WordSimple wordSimple : result.getWordList()) {
                    WordSimple word = wordSimple;
                    sb.append(word.getWords());
                    sb.append("\n");
                }
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recWebimage(Context ctx, String filePath, final ServiceListener listener) {
        GeneralBasicParams param = new GeneralBasicParams();
        param.setDetectDirection(true);
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeWebimage(param, new OnResultListener<GeneralResult>() {
            @Override
            public void onResult(GeneralResult result) {
                StringBuilder sb = new StringBuilder();
                for (WordSimple wordSimple : result.getWordList()) {
                    WordSimple word = wordSimple;
                    sb.append(word.getWords());
                    sb.append("\n");
                }
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recBankCard(Context ctx, String filePath, final ServiceListener listener) {
        BankCardParams param = new BankCardParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeBankCard(param, new OnResultListener<BankCardResult>() {
            @Override
            public void onResult(BankCardResult result) {
                /*String res = String.format("卡号：%s\n类型：%s\n发卡行：%s\n有效日期：%s\n持有者姓名：%s",
                        result.getBankCardNumber(),
                        result.getBankCardType().name(),
                        result.getBankName(),
                        result.getValid_date(),
                        result.getHolder_name());
                listener.onResult(res);*/
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recVehicleLicense(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeVehicleLicense(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recDrivingLicense(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeDrivingLicense(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recLicensePlate(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeLicensePlate(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recBusinessLicense(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeBusinessLicense(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recReceipt(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        param.putParam("detect_direction", "true");
        OCR.getInstance(ctx).recognizeReceipt(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recTaxireceipt(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeTaxireceipt(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recVincode(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeVincode(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recTrainticket(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeTrainticket(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recPassport(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizePassport(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recVatInvoice(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeVatInvoice(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recQrcode(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeQrcode(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recNumbers(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeNumbers(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recLottery(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeLottery(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recBusinessCard(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeBusinessCard(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recHandwriting(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeHandwriting(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recCustom(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.putParam("templateSign", "");
        param.putParam("classifierId", 0);
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recognizeCustom(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recTripTicket(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recoginzeTripTicket(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recCarSellInvoice(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recoginzeVihickleSellInvoice(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recVihicleCertification(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recoginzeVihickleCertificate(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recExampleDoc(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recoginzeExampleDoc(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recWrittenText(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recoginzeWrittenText(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recHuKouPage(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recoginzeHuKouPage(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recNormalMachineInvoice(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recoginzeNormalMachineInvoice(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recweightnote(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recoginzeweightnote(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void recmedicaldetail(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recoginzemedicaldetail(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }

    private static void reconlinetaxiitinerary(Context ctx, String filePath, final ServiceListener listener) {
        OcrRequestParams param = new OcrRequestParams();
        param.setImageFile(new File(filePath));
        OCR.getInstance(ctx).recoginzeonlinetaxiitinerary(param, new OnResultListener<OcrResponseResult>() {
            @Override
            public void onResult(OcrResponseResult result) {
                listener.onResult(result.getJsonRes());
            }

            @Override
            public void onError(OCRError error) {
                listener.onResult(error.getMessage());
            }
        });
    }
}
